package dictionary

import (
    "github.com/gin-gonic/gin"
    "github.com/jinzhu/copier"
    "iip/dictionary/model"
)

func (s *Dictionary) FactoryCreate(c *gin.Context) {
    req := &struct {
        Name            string `form:"name"          json:"name"          binding:"required"`
        Description     string `form:"description"   json:"description"   binding:"required"`
        LocationID      int64  `form:"location_id"   json:"location_id"   binding:"required"`
        Supervisor      string `form:"supervisor"    json:"supervisor"    binding:"required"`
        SupervisorPhone string `form:"supervisor_phone" json:"supervisor_phone" binding:"required"`
    }{}
    if err := c.ShouldBind(req); err != nil {
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info(req, req.LocationID)


    loc, err := s.GetLocationByID(req.LocationID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Infof("find loc[%d]=%s", loc.ID, loc.Name)
    }


    row := model.DictFactory{}
    _ = copier.Copy(&row, req)
    row.ID = s.idgen.NextID()
    if err := s.gorm().Create(&row).Error; err != nil {
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info("create factory done")
    s.json_data(c, s.factory_full(&row, loc))
}

func (s *Dictionary) FactoryDelete(c *gin.Context) {
    req := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    if err := s.gorm().Delete(&model.DictFactory{ID: req.ID}).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info("delete factory by id ", req.ID)
    s.json_ok(c)
}

func (s *Dictionary) FactoryPagedList(c *gin.Context) {

    query := &struct {
        Page     int    `form:"page" binding:"required,gte=1" json:"page"`
        PageSize int    `form:"pagesize" binding:"required,gte=1" json:"pagesize"`
        Order    string `form:"order" json:"order"`
    }{}
    if err := c.ShouldBindQuery(query); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    if query.Order != "asc" && query.Order != "desc" {
        query.Order = "asc"
    }


    var total int64
    if err := s.gorm().Model(&model.DictFactory{}).Count(&total).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    var com_lst []*model.DictFactory
    if err := s.gorm().Limit(query.PageSize).Offset(query.PageSize * (query.Page - 1)).Order("id " + query.Order).Find(&com_lst).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    var lst []interface{}
    for _, com := range com_lst {
        loc, _ := s.GetLocationByID(com.LocationID)
        lst = append(lst, s.factory_full(com, loc))
    }
    s.logger.Info("query factory paged list")
    s.json_paged_data(c, total, query.Page, query.PageSize, lst)
}

func (s *Dictionary) FactoryByID(c *gin.Context) {
    req := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    com, err := s.GetFactoryByID(req.ID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    loc, err := s.GetLocationByID(com.LocationID)
    if err != nil {
        s.logger.Error(err)
        loc = &model.DictLocation{}
    }

    s.logger.Info("query factory by id ", req.ID)
    s.json_data(c, s.factory_full(com, loc))
}

func (s *Dictionary) FactoryUpdate(c *gin.Context) {

    req_id := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req_id); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    com, err := s.GetFactoryByID(req_id.ID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }


    var row model.DictFactory
    if err := c.BindJSON(&row); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    row.ID = com.ID
    row.CreatedAt = com.CreatedAt
    row.UpdatedAt = com.UpdatedAt
    row.DeletedAt = com.DeletedAt


    loc, err := s.GetLocationByID(row.LocationID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    if err := s.db.Save(row).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Info("update factory id=", row.ID)
        s.json_data(c, s.factory_full(&row, loc))
    }
}
